package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.article.feign.AdminFeign;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.HotArticlesService;
import com.heima.common.constants.article.ArticleConstans;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vos.HotArticleVo;
import com.heima.model.common.dtos.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.hbase.thirdparty.com.google.common.collect.Comparators;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class HotArticlesServiceImpl implements HotArticlesService {
    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private AdminFeign adminFeign;

    @Override
    public void computeHotArticle() {
        String dayParam = new DateTime().minusDays(5).toString("yyyy-MM-dd hh:mm:ss");
        System.out.println(dayParam);
        List<ApArticle> apArticles = apArticleMapper.loadArticleForhot(dayParam);
        System.out.println(apArticles+"apArticles");
        List<HotArticleVo> list =  computeHotArticle(apArticles);
        cacheToRedisByTag(list);

    }

    private void cacheToRedisByTag(List<HotArticleVo> list) {
        if (list==null) {
            log.error("计算热点文章失败，热点文章列表为空");
            return;
        }
        ResponseResult responseResult = adminFeign.selectAllChannel();
        List<AdChannel> adChannels = JSON.parseArray(JSON.toJSONString(responseResult.getData()), AdChannel.class);
        System.out.println(list+"list");
        adChannels.stream().forEach(channel->{
            List<HotArticleVo> collect = list.stream().filter(vo -> vo.getChannelId().equals(channel.getId())).collect(Collectors.toList());
            sortAndCache(collect,ArticleConstans.HOT_ARTICLE_FIRST_PAGE+channel.getId());
        });
        sortAndCache(list,ArticleConstans.HOT_ARTICLE_FIRST_PAGE+ArticleConstans.DEFAULT_TAG);

    }

    private void sortAndCache(List<HotArticleVo> collect, String cacheStr) {
        collect= collect.stream().sorted(Comparator.comparing(HotArticleVo::getScore).reversed()).limit(30).collect(Collectors.toList());
        stringRedisTemplate.opsForValue().set(cacheStr,JSON.toJSONString(collect));
    }

    private List<HotArticleVo> computeHotArticle(List<ApArticle> apArticles) {
        return apArticles.stream().map(article->{
            HotArticleVo hotArticleVo = new HotArticleVo();
            BeanUtils.copyProperties(article,hotArticleVo);
            hotArticleVo.setScore(computeScore(article));
            return hotArticleVo;
        }).collect(Collectors.toList());
    }

    private Integer computeScore(ApArticle article) {
        Integer score = 0 ;
        if (article.getLikes()!=null)
            score += article.getLikes() * ArticleConstans.HOT_ARTICLE_LIKE_WEIGHT;
        if (article.getCollection()!=null)
            score +=article.getCollection() * ArticleConstans.HOT_ARTICLE_COLLECTION_WEIGHT;
        if (article.getComment()!=null)
            score +=article.getComment() * ArticleConstans.HOT_ARTICLE_COMMENT_WEIGHT;
        if (article.getViews()!=null)
            score += article.getViews();
        return score;

    }
}
